<template>
  <div class="active">
    <div
      style="display: flex; align-items: center; justify-content: space-between"
    >
      <h2>供应商价格评审</h2>
      <el-button type="primary" @click="getInfo(true)">刷新</el-button>
    </div>
    <div class="table" ref="tablediv">
      <el-table :data="table" style="width: 100%">
        <!-- 使用 v-for 循环生成表格列 -->

        <el-table-column prop="id" label="序号" width="80" align="center">
          <template #default="scope">
            <span class="ellipsis">{{ scope.row.key }}</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="name"
          label="报名供应商"
          width="200"
          align="center"
        >
          <template #default="scope">
            <span class="ellipsis">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" width="200" align="center">
          <template #default="scope">
            <span class="link" @click="handleView(scope.$index, scope.row)"
              >查看</span
            >
          </template>
        </el-table-column>

        <el-table-column
          label="报价"
          align="center"
          v-if="
            selectedFormula == 'equation_floor_price' ||
            selectedFormula == 'equation_custom'
          "
        >
          <template #default="scope">
            <span>￥{{ scope.row.price || 0 }}元</span>
          </template>
        </el-table-column>
        <el-table-column
          label="下浮率"
          align="center"
          v-else-if="selectedFormula == 'equation_float_prob'"
        >
          <template #default="scope">
            <div class="price-cell">
              {{scope.row.price}}
              <span>%</span>
            </div>
          </template>
        </el-table-column>
        <!-- 手动添加操作列 -->
        <el-table-column
          label="拆算得分"
          width="120"
          fixed="right"
          align="center"
        >
          <template #default="scope">
            <span v-if="selectedFormula != 'equation_custom'">{{ scope.row.score>0?scope.row.score:0 }}分</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="endbutton">
      <el-button type="primary" @click="nextStep()">下一步</el-button>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { judgeProjectInfo } from "@/api/project.js";
import {
  getDictList,
  getDict,
  nowTime,
  getdict,
  SearchDicte,
} from "@/utils/common.js";
import {
  execTask,
  applySupplierInfoByProjectId,
  updateSupplierPushValue,
  updateApplySupplier,
} from "@/api/examine.js";

// 定义 emit 函数
const emit = defineEmits(["next-step"]);
const route = useRoute();
const router = useRouter();
const tablediv = ref({});
const id = computed(() => route.query.id);
const selectedFormula = ref('');
const props = defineProps({
  type: {
    type: String,
    required: true,
  },
  projectInfo:{
    type: String,
    required: true,
  }
});

// 定义表格列配置
const getInfo = (boolean) => {
  applySupplierInfoByProjectId({ id: id.value }).then((res) => {
    if (res.code == "200") {
      table.value = [];
      let key=0
      for (let i = 0; i < res.data.length; i++) {
        if(res.data[i].status=='apply_supplier_pass'){
          key++
        table.value[i] = {
          key: key,
          ...res.data[i],
          price: formatPrice(res.data[i].price)
        };
      }
      }
      if (boolean) {
          ElMessage({
            message: "刷新成功",
            type: "success",
          });
        }
    }
  });
  
  selectedFormula.value=props.projectInfo.priceEquation?props.projectInfo.priceEquation:'equation_floor_price'
};
function formatPrice(price) {
  if (price == null) return ''; // 如果价格为空或未定义，则返回空字符串

  const num = parseFloat(price);
  if (isNaN(num)) return ''; // 如果价格不是有效的数字，则返回空字符串

  // 判断是否需要使用科学记数法
  if (Math.abs(num) >= 1e6) { // 例如，当数值超过一百万时使用科学记数法
    return num.toExponential(2); // 保留两位小数的科学记数法表示
  } else {
    // 对于较小的数值，使用本地化字符串格式，带逗号分隔符，保留两位小数
    return num.toLocaleString(undefined, {
      minimumFractionDigits: 2,
      maximumFractionDigits: 2
    });
  }
}
const nextStep = () => {
  // 触发自定义事件并传递数据（如果需要）
  if (props.type == "taskPrice") {
    window.location.replace(""); 
  } else {
    emit("next-step", 4);
  }
};
const handleView = (item, data) => {
  console.log(item, data);
  const url = `/judgedetail?id=${id.value}&type=judgeInfo&judgeid=${data.id}`;
  window.open(url, "_blank"); // 在新窗口中打开
};
// 定义表格数据
const table = ref([]);
onMounted(() => {
  getInfo();
});
</script>
<style scoped lang="scss">
@use "./judge";
</style>
